package com.mmall.controller.portal;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserservice;
import org.omg.CORBA.Object;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by MHT on 2017/7/3.
 */
@Controller
@RequestMapping("/user/")
public class UserController
{
    @Autowired
    private IUserservice iUserservice;
    /**
     * 用户登陆
     *
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping(value = "login.do", method = RequestMethod.POST)
    @ResponseBody  //自动通过SpringMVC的插件将返回值序列化为Json
    //登录接口
    public ServerResponse<User> login(String username, String password, HttpSession session) {
        //service-->mybatis->dao
        ServerResponse<User> response = iUserservice.login(username, password);
        if (response.isSuccess()) {
            session.setAttribute(Const.CURRENT_USER, response.getData());
        }
        return response;
    }

    //登出接口
    @RequestMapping(value = "logout.do", method = RequestMethod.POST)
    @ResponseBody  //自动通过SpringMVC的插件将返回值序列化为Json
    public ServerResponse<String> logout(HttpSession session) {
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccess("当前用户已退出");
    }

    //注册接口
    @RequestMapping(value = "register.do", method = RequestMethod.POST)
    @ResponseBody  //自动通过SpringMVC的插件将返回值序列化为Json
    public ServerResponse<String> register(User user) {
        return iUserservice.register(user);
    }

    //校验接口
    @RequestMapping(value = "check_valid.do", method = RequestMethod.POST)
    @ResponseBody  //自动通过SpringMVC的插件将返回值序列化为Json
    public ServerResponse<String> checkValid(String str,String type){
        return iUserservice.checkValid(str,type);
    }

    //获取用户登录信息
    @RequestMapping(value = "get_user_info.do", method = RequestMethod.POST)
    @ResponseBody  //自动通过SpringMVC的插件将返回值序列化为Json
    public ServerResponse<User> getUserinfo(HttpSession session){
        User user=(User)session.getAttribute(Const.CURRENT_USER);
        if (user!=null){
            return ServerResponse.createBySuccess(user);
        }
        return ServerResponse.createByErrorMessage("用户未登录，无法获取当前用户的信息");
    }

    //忘记密码
    @RequestMapping(value = "forget_get_question.do", method = RequestMethod.POST)
    @ResponseBody  //自动通过SpringMVC的插件将返回值序列化为Json
    public ServerResponse<String> forgetGetQuestion(String username){
        return iUserservice.selectQuestion(username);
    }

    //校验问题答案是否正确    把token放到String里面
    @RequestMapping(value = "forget_check_answer.do", method = RequestMethod.POST)
    @ResponseBody  //自动通过SpringMVC的插件将返回值序列化为Json
    public ServerResponse<String> forgetCheckAnswer(String username,String question,String answer){
        //用本地的guwa缓存做token（有效期）
        return iUserservice.CheckAnswer(username,question,answer);
    }

    //忘记密码中的重置密码功能
    @RequestMapping(value = "forget_reset_password.do", method = RequestMethod.POST)
    @ResponseBody  //自动通过SpringMVC的插件将返回值序列化为Json
    public ServerResponse<String> forgetResetPassword(String username,String passwordNew,String forgetToken){
        return iUserservice.forgetResetPassword(username,passwordNew,forgetToken);
    }

    //登录状态下的重置密码功能
    @RequestMapping(value = "reset_password.do", method = RequestMethod.POST)
    @ResponseBody  //自动通过SpringMVC的插件将返回值序列化为Json
    public ServerResponse<String> resetPassword(HttpSession session,String passwordOld,String passwordNew){
        User user=(User)session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        return iUserservice.resetPassword(passwordOld,passwordNew,user);
    }

    //更新用户个人信息功能
    @RequestMapping(value = "update_information.do", method = RequestMethod.POST)
    @ResponseBody  //自动通过SpringMVC的插件将返回值序列化为Json
    public ServerResponse<User> updateinformation(HttpSession session,User user){
        User currentUser=(User)session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null){
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        user.setId(currentUser.getId());
        user.setUsername(currentUser.getUsername());

        ServerResponse<User> response = iUserservice.updateinformation(user);
        if (response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }

    //获取用户详细信息功能
    @RequestMapping(value = "get_information.do", method = RequestMethod.POST)
    @ResponseBody  //自动通过SpringMVC的插件将返回值序列化为Json
    public ServerResponse<User> get_information(HttpSession session){
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要强制登录 status=10");
        }
        return iUserservice.getinformation(currentUser.getId());
        //service里的逻辑就是将密码置为空，这样前台的用户就能看到自己的详细信息，也就能够进行用户信息的修改
    }







}
